package PX.选择排序;

import java.util.Arrays;

public class Selection_Sort {
    public static void main(String[] args) {
        //测试一把
        int[] arr = new int[]{100,-1,2,1,20,13,12};
        int[] ints = selcetionSort(arr);
        System.out.println(Arrays.toString(ints));
    }

    public static int[] selcetionSort(int[] arrs) {
        if (arrs.length == 0){
            return arrs;
        }
        //每次循环都从当下开始，找（选择）一个最小的和自己交换位置
        for (int i = 0; i <  arrs.length; i++) {
            int miniIndex = i;
            for (int j = i; j < arrs.length; j++) {
                if (arrs[j]<arrs[miniIndex]){
                    miniIndex = j;
                }
            }
            //经典三段式交换
            int temp = arrs[miniIndex];
            arrs[miniIndex] = arrs[i];
            arrs[i] = temp;
        }
        return arrs;
    }
}
